package demo.annotation;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


/**
 * @author : GuoFei
 * @date : 2018/12/14 17:18
 */
@Aspect
@Component
public class SoutParamHelper {

    @Around(value = "@annotation(soutParam)||@within(soutParam)")
    public void around(ProceedingJoinPoint pjp, SoutParam soutParam) {
        try {
            Signature sig = pjp.getSignature();
            MethodSignature msig = null;
            // 有可能该注解用于接口
            if (!(sig instanceof MethodSignature)) {
                throw new IllegalArgumentException("该注解只能用于方法");
            }
            if (soutParam.sout()) {
                msig = (MethodSignature) sig;
                Object target = pjp.getTarget();
                Class currentClazz = target.getClass();
                Method currentMethod = currentClazz.getMethod(msig.getName(), msig.getParameterTypes());
                System.out.println(currentClazz.getName());
                System.out.println(currentMethod.getName());
                System.out.println(JSON.toJSONString(pjp.getArgs()));
                pjp.proceed();
            } else {
                System.out.println("不要打印参数");
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

//    @Pointcut(value = "execution(public * demo.retry.*.*(..)) && @annotation(sout)||( @within(sout))")
//    public void pointCut( SoutParam sout){}

}
